﻿using Leo.Awesome.DictionaryManagement.Domain;
using Leo.Awesome.DictionaryManagement.DomainServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;


namespace Leo.Awesome.Infrastructure.Seeds.DicItem
{
    public class SectionDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly ISectionDataSeeder sectionDataSeeder;
        public SectionDataSeedContributor(ISectionDataSeeder sectionDataSeeder)
        {
            this.sectionDataSeeder = sectionDataSeeder;
        }
        public async Task SeedAsync(DataSeedContext context)
        {
            var readSections = await ReadSections(".//Seeds//DicItem//DictionaryItemDataSeed.json");
            var sectionsList = readSections.ToList(node => node.Items);
            await sectionDataSeeder.SeedAsync(sectionsList, AwesomeInfrastructureServcieConsts.Domain);
            await Task.CompletedTask;
        }

        public async Task<List<Section>> ReadSections(string filePath)
        {
            var jsonContent = System.IO.File.ReadAllText(filePath);
            var sections = JsonConvert.DeserializeObject<List<Section>>(jsonContent);
            var sectionsFromDb = await sectionDataSeeder.GetAllAsync(AwesomeInfrastructureServcieConsts.Domain);
            var results = ReadSectionsRecursive(sections, sectionsFromDb, null);
            return results;
        }

        private static List<Section> ReadSectionsRecursive(List<Section> sections,
            List<Section> sectionsFromDb,
            Section parent = null)
        {
            var results = new List<Section>();
            var index = 1;
            foreach (var section in sections)
            {

                var id = Guid.Empty;
                var pathTree = string.Empty;
                Guid? parentId = null;
                var sectionFromDb = sectionsFromDb.FirstOrDefault(x => x.Code == section.Code);
                if (sectionFromDb != null)
                {
                    id = sectionFromDb.Id;
                    pathTree = sectionFromDb.TreePath;
                    parentId = sectionFromDb.ParentId;
                }
                else 
                {
                    id = Guid.NewGuid();
                    pathTree = parent?.TreePath + id + "/";
                    parentId = parent?.Id;
                }
                var rootSection = new Section(
                    id,
                    parentId, 
                    section.Name, 
                    section.Code, 
                    pathTree, 
                    index, 
                    true,
                    "",
                    section.IsSection);
                results.Add(rootSection);

                if (section.Items != null && section.Items.Count > 0)
                {
                    var childSections = ReadSectionsRecursive(section.Items, 
                        sectionsFromDb, rootSection);

                    rootSection.Items.AddRange(childSections);
                }
                index += 10;
            }

            return results;
        }
    }
}
